package com.hengpeng.itfin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfin.domain.vo.AccountLogVO;
import com.hengpeng.itfin.service.AccountLogService;
import com.hengpeng.itfin.service.ServiceFactory;
import com.hengpeng.itfinbase.en.AccountType;
import com.hengpeng.itfinbase.en.TradeMode;
import com.hengpeng.itfinbase.en.TransType;
import com.hengpeng.itfinbase.persist.AccountLogReport;
import com.hengpeng.itfinbase.persist.SystemConfig;

@Service
@Lazy
public class AccountLogServiceImpl implements AccountLogService {
    private final static String paltDaily = "paltformDaily";

    private static Log logger = LogFactory.getLog(AccountLogServiceImpl.class);

    @Autowired
    private ServiceFactory serviceFactory;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void paltformAccountDaily() {
        SystemConfig config = serviceFactory.getPersistService().lockObject(SystemConfig.class, paltDaily);
        if (config != null) {
            Date start = DateTimeUtil.formatToDate(config.getValue(), "yyyy-MM-dd");
            String curDate = DateTimeUtil.formatToStr(new Date(), "yyyy-MM-dd");// 当天日期
            Date end = DateTimeUtil.formatToDate(curDate, "yyyy-MM-dd");
            if (start.getTime() < end.getTime()) {
                String[] transType = new String[] { TransType.CREDITASSIGN_FEE.toString(),
                        TransType.LOAN_FEE.toString(), TransType.DRAW_FEE.toString(), TransType.IMPREST_FEE.toString(),
                        TransType.ADDMONEY.toString() };
                List<AccountLogVO> list = serviceFactory.getDaoFactory().getStaticJDBCDao()
                        .getPlatAccountTrade(start, end, transType);
                List<AccountLogReport> reports = new ArrayList<AccountLogReport>();
                for (AccountLogVO accountLog : list) {
                    AccountLogReport report = new AccountLogReport();
                    report.setAccountType(AccountType.PLATFORM);
                    report.setTransType(accountLog.getTransType());
                    report.setReportDate(DateTimeUtil.formatToDate(accountLog.getReportTime(), "yyyy-MM-dd"));
                    if (accountLog.getTradeMode().equals(TradeMode.ADD)) {
                        report.setTransMoney(-accountLog.getMoney());
                    }
                    if (accountLog.getTradeMode().equals(TradeMode.SUBTRACT)) {
                        report.setTransMoney(accountLog.getMoney());
                    }
                    reports.add(report);
                }
                serviceFactory.getPersistService().saveAllObject(reports);
                config.setValue(curDate);
                serviceFactory.getPersistService().update(config);
                logger.info("平台账户日结统计执行结束,产生日结记录数:" + reports.size());
            }
        } else {
            config = new SystemConfig();
            config.setId(paltDaily);
            config.setVersion(0L);
            config.setValue("2010-01-01");
            config.setDescription("平台账户交易日结最后统计日期");
            serviceFactory.getPersistService().save(config);
        }
        logger.info("平台账户日结统计执行结束,结束日期:" + config.getValue());
    }

    public static void main(String[] args) {
        Date end = DateTimeUtil.formatToDate(DateTimeUtil.formatShortStr(new Date()), "yyyy-MM-dd");
        System.out.println(end);
    }
}
